package net.kylewu;

import java.util.Iterator;

import net.kylewu.object.FanfouMessage;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * @author Kyle Wu
 *
 */
public class RCFFMessageReader {

	@SuppressWarnings("unchecked")
	public static FanfouMessage getLatestMessage(String url)
			throws DocumentException {

		SAXReader reader = new SAXReader();
		Document document = reader.read(url);

		Element root = document.getRootElement();

		root = root.element("channel");

		for (Iterator i = root.elementIterator("item"); i.hasNext();) {
			Element latestItem = (Element) i.next();

			FanfouMessage ffm = new FanfouMessage();

			ffm.setText(latestItem.elementText("title"));

			/*
			 * SimpleDateFormat formatter = new
			 * SimpleDateFormat("EE, dd mon yyyy HH:mm:ss ZZ"); Date strtodate =
			 * null; try { strtodate =
			 * formatter.parse(latestItem.elementText("pubDate")); } catch
			 * (ParseException e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); }
			 * 
			 * ffm.setPubDate(strtodate); System.out.println();
			 */
			return ffm;

		}

		return null;

	}

}
